//
//  ViewController.m
//  Parctice 滑条问题
//
//  Created by 冀永金 on 16/7/16.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "CellOfFirst.h"
#import "CellOfSmall.h"
#import "SelfSizeTool.h"
#import "UIView+Frame.h"
#define WIDTH self.view.frame.size.width
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collection;
@property (nonatomic, strong) UICollectionView *smallOfCollection;
@property (nonatomic, assign) NSInteger labelIndex;// 记录是第几个label
@property (nonatomic, strong) NSIndexPath *oldIndexPath;//记录第几个label的indexpath
@property (nonatomic,strong) UIView *redView;
@property (nonatomic, strong) NSMutableArray *marr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;

   self.oldIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    //self.labelIndex = 0;
     self.marr = [NSMutableArray arrayWithObjects:@"新闻",@"视频",@"音乐",@"兴趣",@"呵呵",@"哈哈",@"嘻嘻",@"嘿嘿", @"是司机",@"开车啦啦",nil];
    [self config];
    [self createRedView];
    [self createKVOAction];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)config {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 55);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 55, self.view.frame.size.width, self.view.frame.size.height - 55) collectionViewLayout:layout];
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.pagingEnabled = YES;
    [self.collection registerClass:[CellOfFirst class] forCellWithReuseIdentifier:@"pool"];
    
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc]init];
    layout1.itemSize = CGSizeMake(self.view.frame.size.width / 5, 50);
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout1.minimumLineSpacing = 0;
    self.smallOfCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 5, self.view.frame.size.width, 50) collectionViewLayout:layout1];
    [self.view addSubview:self.smallOfCollection];
    self.smallOfCollection.backgroundColor = [UIColor whiteColor];
    self.smallOfCollection.delegate = self;
    self.smallOfCollection.dataSource = self;
    [self.smallOfCollection registerClass:[CellOfSmall class] forCellWithReuseIdentifier:@"smallPool"];
    self.smallOfCollection.showsHorizontalScrollIndicator = NO;
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.collection) {
        return 10;
    }
    else {
        return 10;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collection) {
        CellOfFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1.0f];
        return cell;
    }
    else  {
        CellOfSmall *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"smallPool" forIndexPath:indexPath];
       
       
       // cell.label.textColor = [UIColor blackColor];
        cell.label.textAlignment = 1;
        //保证开启程序第一个字是红色的 并且字体为大字
        if (indexPath.row == _labelIndex) {
            cell.label.textColor = [UIColor redColor];
            cell.label.font = [UIFont systemFontOfSize:18];
        }else {
            cell.label.font = [UIFont systemFontOfSize:15];
            cell.label.textColor = [UIColor blackColor];
        }
        if (indexPath.row == _oldIndexPath.row) {
            cell.label.textColor = [UIColor redColor];
            cell.label.font = [UIFont systemFontOfSize:18];
        }else {
            cell.label.font = [UIFont systemFontOfSize:15];
            cell.label.textColor = [UIColor blackColor];

        }
          cell.label.text = _marr[indexPath.item];
        
        //cell.backgroundColor = [UIColor redColor];
        return cell;
    }
}
- (void)createRedView {
    self.redView =[[UIView alloc]initWithFrame:CGRectMake(0, 53, self.view.frame.size.width / 5, 2)];
    [self.view addSubview:_redView];
    _redView.backgroundColor = [UIColor redColor];
}
// 当滑动已经减速时进行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collection) {
        // 当前大collectionView 第几个item
        NSInteger currentItemCount = scrollView.contentOffset.x / WIDTH;
        // 根据第几个item获取indexPath
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentItemCount inSection:0];
        // 根据indexPath 获取当前的小collectionViewCell
        // 进行强转
        CellOfSmall *currentCell = (CellOfSmall *)[_smallOfCollection cellForItemAtIndexPath:indexPath];
        // 然后将字体变红
        currentCell.label.textColor = [UIColor redColor];
        currentCell.label.font = [UIFont systemFontOfSize:18];
        /** 恢复字体颜色*/
        // 根据之前，再取出cell协议的方法中记录的item下标
        NSIndexPath *indexPathPast = [NSIndexPath indexPathForItem:_labelIndex inSection:0];
        NSLog(@"%ld",self.labelIndex);
        
        /** 加判断防止划一点点就松开,  并没有去到新页面的情况,  则文字不变色 */
        if (indexPath != indexPathPast) {
            // 根据indexPath获取当前的小collectionViewCell
            
            CellOfSmall *pastCell = (CellOfSmall *)[self.smallOfCollection cellForItemAtIndexPath:indexPathPast];
            pastCell.label.textColor = [UIColor blackColor];
            pastCell.label.font = [UIFont systemFontOfSize:15];
        }
        self.labelIndex = currentItemCount;
        self.oldIndexPath = indexPath;
    
    }
    
}
#pragma mark -------添加观察者
- (void)createKVOAction {
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"nil"];
}

// 添加观察者之后 自动调用此方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    CGFloat x = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat redX = x / 5;
    if (x <= WIDTH) {
        self.redView.transform = CGAffineTransformMakeTranslation(redX, 0);
        
    }
    else if (x > 6 *WIDTH) {
        self.redView.transform = CGAffineTransformMakeTranslation((redX - 6 * WIDTH / 5) + WIDTH / 5, 0);
        
    }
    
    else {
        self.smallOfCollection.contentOffset = CGPointMake(redX - WIDTH / 5, 0);
    }
    
}
// 点击小cell触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.smallOfCollection) {
        [self.collection scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        CellOfSmall *cell = (CellOfSmall *)[_smallOfCollection cellForItemAtIndexPath:indexPath];
        cell.label.textColor = [UIColor redColor];
        cell.label.font = [UIFont systemFontOfSize:18];
        CellOfSmall *pastCell = (CellOfSmall *)[_smallOfCollection cellForItemAtIndexPath:_oldIndexPath];
        if (indexPath != _oldIndexPath) {
            pastCell.label.textColor = [UIColor blackColor];
            pastCell.label.font = [UIFont systemFontOfSize:15];
        }
        self.oldIndexPath = indexPath;
        self.labelIndex = _oldIndexPath.item;
    }

}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (collectionView == _smallOfCollection) {
//        NSString *str = [self.marr objectAtIndex:indexPath.row];
//        CGFloat f = [SelfSizeTool selfSizeWidthString:str height:40 font:[UIFont systemFontOfSize:15]];
//        [UIView animateWithDuration:0.5 animations:^{
//            self.redView.yj_width = f + 10;
//        }];
//        return CGSizeMake(f + 10, 40);
//    }else {
//        return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 55);
//    }
//    
//}
//



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
